define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                searchFormVisible: true,
                showToggle: false,
                showColumns: false,
                search:false,
                showExport: false,
                showSearch: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'level', title: __('Level'), searchList: {1: __('LV1'), 2: __('LV2'), 3: __('LV3'), 4: __('LV4'), 5: __('LV5')}, formatter: Table.api.formatter.normal},
                        {field: 'gender', title: __('Gender'), searchList: {1: __('Male'), 2: __('Female'),  3: __('未知')}, formatter: Table.api.formatter.normal},
                        {field: 'age', title: __('Age'), operate: 'BETWEEN'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'number', title: __('Number'), operate: 'LIKE'},
                        {field: 'health', title: __('Health'), searchList: {1: __('Health 1'), 2: __('Health 2')}, formatter: Table.api.formatter.normal},
                        {field: 'health_image', title: __('Health_Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'real', title: __('Real'), searchList: {1: __('Real 1'), 2: __('Real 2')}, formatter: Table.api.formatter.normal},
                        {field: 'work_hour', title: __('WorkHour'), formatter:function(value,row,index){return value + "/" + row.work_times; }, operate: false},
                        {field: 'company.company_name', title: __('Company_Name'), operate: false},
                        {field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange'},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {1: __('Normal'), 2: __('Hidden')}},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'disable',
                                    text: __('禁用'),
                                    title: __('禁用'),
                                    classname: 'btn btn-xs btn-primary btn-ajax',
                                    refresh: true,
                                    hidden: function (row, o) {
                                        if (row.status === 2) return true
                                    },
                                    confirm: '即将禁止该会员在平台报名和参加工作，请确认是否禁用！',
                                    url: 'user/user/disable',
                                    success: function (data) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                },
                                {
                                    name: 'enable',
                                    text: __('启用'),
                                    title: __('启用'),
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    refresh: true,
                                    hidden: function (row, o) {
                                        if (row.status === 1) return true
                                    },
                                    url: 'user/user/enable',
                                    success: function (data) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                },
                            ]
                        },
                    ]
                ]
            });

            // 批量启用
            $(".btn-normal").on("click", function () {
                let data = table.bootstrapTable('getSelections');
                let ids = data.map(x => {
                    return x.id
                }).join(',')
                $.post({
                    url: 'user/user/batch?act=1&ids=' + ids,
                    success: function (res) {
                        if (res.code === 0) {
                            Toastr.error(res.msg);
                        } else {
                            Toastr.success(res.msg);
                        }
                        $(".btn-refresh").trigger("click");
                    }
                })
            })

            // 批量禁用
            $(".btn-hidden").on("click", function () {
                let data = table.bootstrapTable('getSelections');
                let ids = data.map(x => {
                    return x.id
                }).join(',')
                $.post({
                    url: 'user/user/batch?act=2&ids=' + ids,
                    success: function (res) {
                        if (res.code === 0) {
                            Toastr.error(res.msg);
                        } else {
                            Toastr.success(res.msg);
                        }
                        $(".btn-refresh").trigger("click");
                    }
                })
            })

            // 批量分组
            $("#company").on("click", function () {
                let data = table.bootstrapTable('getSelections');
                let ids = data.map(x => {
                    return x.id
                }).join(',')
                layer.open({
                    type: 1,
                    content: $("#group"),
                    zIndex: 2,
                    title: '请选择分组（公司）',
                    area:['360px','200px'],
                    success: function (dom,index){
                        $("input[name=ids]").val(ids)
                        Form.api.bindevent($("form[role=form]"),function (d,s){
                            layer.close(index)
                            $(".btn-refresh").trigger("click");
                        });
                    }
                })
            })

            // 批量等级
            $("#level").on("click", function () {
                let data = table.bootstrapTable('getSelections');
                let ids = data.map(x => {
                    return x.id
                }).join(',')
                layer.open({
                    type: 1,
                    content: $("#level-form"),
                    zIndex: 2,
                    title: '请选择等级',
                    area:['360px','250px'],
                    success: function (dom,index){
                        $("input[name=ids-level]").val(ids)
                        Form.api.bindevent($("form[role=form]"),function (d,s){
                            layer.close(index)
                            $(".btn-refresh").trigger("click");
                        });
                    }
                })
            })

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});